package com.example.websocketdemo.websocketConfig;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * TODO
 *
 * @ClassName WsMessageDecoder
 * @Author xiaojiang
 * @Date 2021/9/30 上午 11:22
 */
public class WsMessageDecoder  implements  Decoder.Text<String>{

    private static Logger logger = LoggerFactory.getLogger(WsMessageDecoder.class) ;
    private static Set<String> msgTypes = new HashSet<>() ;

    // 在这里可以配置前端传来的数据，转为你需要的对象，由于我不用处理接收的消息或者是String，就不用设置
    @Override
    @SuppressWarnings("unchecked")
    public String decode(String s) throws DecodeException {
        ObjectMapper mapMapper= new ObjectMapper();
        try {
            String json="";
            json=mapMapper.writeValueAsString(s);
            return  json;
        } catch (IOException e) {
            e.printStackTrace();
            return "false";
        }
    }

    // 该方法判断消息是否可以被解码（转换）
    @Override
    @SuppressWarnings("unchecked")
    public boolean willDecode(String s) {

        return true ;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
